import { trim, isEmpty } from 'lodash';
import { Op } from 'sequelize';
import BaseController from './base';
import ReservationPrepayModel from '../models/reservation-prepay';
import { getSequelize } from '../db';

// order docs https://sequelize.org/docs/v6/core-concepts/model-querying-basics/
class ReservationPrepayController extends BaseController {
  getModel() {
    return ReservationPrepayModel;
  }

  /**
   * 解析查询条件
   * @returns {Object}
   */
  getFindWhere(req) {
    const {
      openid, room, primary_confirm_number, confirm_number, start, end
    } = req.query;

    const { hotel_id } = req.user;
    const sequelize = getSequelize();

    const where = {
      hotel_id,
      business_date: {
        [Op.between]: [start, end]
      }
    };

    if (openid) {
      where.openid = openid;
    }

    if (room) {
      where.room = room;
    }

    if (primary_confirm_number) {
      where.primary_confirm_number = primary_confirm_number;
    }

    if (confirm_number) {
      // where.room = room;
      where[Op.and] = sequelize.where(sequelize.fn('FIND_IN_SET', confirm_number, sequelize.col('confirm_number')), '>', 0)
    }

    return { where };
  }
}

export default new ReservationPrepayController();
